/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
/**
 *
 * @author Trung
 */
public class Connection extends javax.swing.JFrame {

    /**
     * Creates new form Connection
     */
    Socket s = null;
    ObjectInputStream ois = null;
    JLabel l = new JLabel();
    JWindow win = new JWindow();
    ImageIcon icon = new ImageIcon();
    JFrame fr = new JFrame();
    String add = null;
    
    public Connection() {
        initComponents();
        
        
    }

    NewJFrame newJFrame = new NewJFrame();
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tf_IP = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        bt_Connect = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        tf_user = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tf_pass = new javax.swing.JTextField();
        bt_Connect1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tf_IP.setText("localhost");

        jLabel1.setText("Server IP:");

        bt_Connect.setText("OK");
        bt_Connect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_ConnectActionPerformed(evt);
            }
        });

        jLabel3.setText("Username");

        tf_user.setText("Username");
        tf_user.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_userActionPerformed(evt);
            }
        });

        jLabel4.setText("Pass");

        tf_pass.setText("Pass");

        bt_Connect1.setText("Cancel");
        bt_Connect1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_Connect1ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setText("Dang Nhap");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 1, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_IP, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tf_pass))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(tf_user, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(59, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(88, 88, 88))
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(bt_Connect, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(bt_Connect1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel2)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_IP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tf_user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tf_pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(59, 59, 59)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_Connect)
                    .addComponent(bt_Connect1))
                .addGap(45, 45, 45))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_ConnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_ConnectActionPerformed
        // TODO add your handling code here:
      /*  try
        {
            Socket sk = new Socket(tf_IP.getText(), 3001);
            
            JOptionPane.showMessageDialog(this, "Connect successful!");
//            newJFrame.setVisible(true);
//            this.setVisible(false);
        }
        catch(UnknownHostException ex)
        {
            System.out.println(ex.getMessage());
        }
        catch(IOException ex)
        {
            System.out.println(ex.getMessage());
        }
        *
        */
        
         try {
         //   add = JOptionPane.showInputDialog(null, "User","Hay nhap ten");           
         //   s = new Socket(add, 20020);
           // ois = new ObjectInputStream(s.getInputStream());
         //   add = JOptionPane.showInputDialog(null, "User","Hay nhap pass");
         //   s = new Socket(add, 20020);
           //ois = new ObjectInputStream(s.getInputStream());
            if (tf_IP.getText()== "localhost")
                tf_IP.setText("127.0.0.1"); 
            add = tf_IP.getText(); //JOptionPane.showInputDialog(null, "Server Address", "192.168.1.2");
            s = new Socket(add, 20020);
            ois = new ObjectInputStream(s.getInputStream());
          //  add = JOptionPane.showInputDialog(null, "Server Address", "192.168.1.2");
            
            //s = new Socket(add, 20020);
          // ois = new ObjectInputStream(s.getInputStream());
           
                        OutputStream os=s.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			String sentMessage="";
                        //InputStream is=s.getInputStream();
			BufferedReader br=new BufferedReader(new InputStreamReader(ois));			
			String receivedMessage=""; 
                        
                        //do
                        for(int i=0;i<2;i++)
			{
                            
                            
                            
                            
                            //CO THU SU DUGN DE GUI 1 CAU TEXT
                            
                            
                            
                            
            if (i==0) sentMessage= tf_user.getText();//JOptionPane.showInputDialog(null, "Nhap User Name", "ten user");		
             if (i==1)sentMessage=  tf_pass.getText();                    //JOptionPane.showInputDialog(null, "Nhap Password", "Password");
				DataInputStream din=new DataInputStream(System.in);
				//sentMessage=din.readLine();
				bw.write(sentMessage);
				bw.newLine();
				bw.flush();
                        }
				
				//if (sentMessage.equalsIgnoreCase("quit"))
					//break;
				//else
				//{                          
 
                                     // JOptionPane.showMessageDialog(null, "haaaaaaaaaaaaaaa")  ;
					//receivedMessage="";
                                        receivedMessage=br.readLine();
					System.out.println("Received : " + receivedMessage);
                                       //br.close();
                                       if (receivedMessage.compareTo("0") ==0)
                                        {
                                          JOptionPane.showMessageDialog(null, "Ban da dang nhap sai ten hoac mat khau")  ;
                                          //s.close();
                                        }
                                        
                                        
                                       else
                                       {
                                            
				     JOptionPane.showMessageDialog(null, "Ban da dang nhap thanh cong")  ;
				
			
			//while(true);
			    
			//bw.close();
        
            
            
            
            
            
          //  fr.setTitle("Displaying " + add + " port 20020");
          // fr.addWindowListener(new Connection.WindowCloser());
           // fr.getContentPane().add(l);
           // l.setIcon(icon);
           // Dimension d = fr.getToolkit().getScreenSize();
            //fr.setSize(d.width,d.height);
          // fr.addKeyListener(this);
           // win.addMouseListener(this);
           // fr.setVisible(true);
            //this.start();
        }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        
        
        
    }//GEN-LAST:event_bt_ConnectActionPerformed

    private void tf_userActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_userActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_userActionPerformed

    private void bt_Connect1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_Connect1ActionPerformed
    this.dispose();
    System.exit(0);     // TODO add your handling code here:
    }//GEN-LAST:event_bt_Connect1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Connection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Connection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Connection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Connection.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Connection().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_Connect;
    private javax.swing.JButton bt_Connect1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField tf_IP;
    private javax.swing.JTextField tf_pass;
    private javax.swing.JTextField tf_user;
    // End of variables declaration//GEN-END:variables
}
